﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MonteCarloAlgorithm.Distribution
{
    public class Triangular
    {
        public static double triangular(double Min, double Mode, double Max, Random r)
        {
            //   Declarations
            double R = 0.0;
            //   Initialise
            
            R = r.NextDouble();       //between 0.0 and 1.0 gaussian
            //    Triangular
            if (R == ((Mode - Min) / (Max - Min)))
            {
                return Mode;
            }
            else if (R < ((Mode - Min) / (Max - Min)))
            {
                return Min + Math.Sqrt(R * (Max - Min) * (Mode - Min));
            }
            else
            {
                return Max - Math.Sqrt((1 - R) * (Max - Min) * (Max - Mode));
            }
        }

        public static int triangular(int Min, int Mode, int Max)
        {
            //   Declarations
            double R = 0;
            //   Initialise
            Random r = new Random();
            R = r.Next(0, 100);       //between 0.0 and 1.0 gaussian
            //    Triangular
            if (R == ((Mode - Min) / (Max - Min)))
            {
                return Mode;
            }
            else if (R < ((Mode - Min) / (Max - Min)))
            {
                return (int)(Min + Math.Sqrt(R * (Max - Min) * (Mode - Min)));
            }
            else
            {
                return (int)(Max - Math.Sqrt((1 - R) * (Max - Min) * (Max - Mode)));
            }
        }



    }
}
